package com.landiao.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.landiao.common.Constant;
import com.landiao.common.utils.CodecUtils;
import com.landiao.entities.ServiceItem;
import com.landiao.entities.ServiceItemPrice;
import com.landiao.entities.ServiceItemRoleRela;
import com.landiao.mapper.ServiceItemMapper;
import com.landiao.mapper.ServiceItemPriceMapper;
import com.landiao.mapper.ServiceItemRoleRelaMapper;
@Service
public class ServiceItemService extends BaseService<ServiceItem, String>{

	@Autowired
	private ServiceItemPriceMapper priceMapper;
	
	@Autowired
	private ServiceItemRoleRelaMapper roleRelaMapper;
	
	@Override
	protected ServiceItemMapper getBaseMapper() {
		// TODO Auto-generated method stub
		return (ServiceItemMapper)baseMapper;
	}

	@Override
	public void save(ServiceItem record) {
		// TODO Auto-generated method stub
		record.setState(Constant.STATE_NORMAL);
		record.setStatedate(new Date());
		this.getBaseMapper().insert(record);
		if(record.getPriceList()!=null&&record.getPriceList().size()>0){
			for(ServiceItemPrice price:record.getPriceList()){
				if(price!=null&&price.getShopGradeId()!=null){
					price.setServiceItemPriceId(CodecUtils.getUUID());
					price.setCompId(record.getCompId());
					price.setServiceItemId(record.getServiceItemId());
					
					this.priceMapper.insert(price);
				}
			}
		}
		if(record.getRoleRelaList()!=null&&record.getRoleRelaList().size()>0){
			for(ServiceItemRoleRela roleRela:record.getRoleRelaList()){
				if(roleRela!=null&&roleRela.getRoleId()!=null){
					roleRela.setServiceItemRoleRelaId(CodecUtils.getUUID());
					roleRela.setCompId(record.getCompId());
					roleRela.setServiceItemId(record.getServiceItemId());
					
					this.roleRelaMapper.insert(roleRela);
				}
			}
		}
	}

	@Override
	public void update(ServiceItem record) {
		// TODO Auto-generated method stub
		this.priceMapper.deleteWithNoPackageByItemId(record.getServiceItemId());
		this.roleRelaMapper.deleteByItemId(record.getServiceItemId());
		record.setState(Constant.STATE_NORMAL);
		record.setStatedate(new Date());
		this.getBaseMapper().updateByPrimaryKey(record);
		if(record.getPriceList()!=null&&record.getPriceList().size()>0){
			for(ServiceItemPrice price:record.getPriceList()){
				if(price!=null&&price.getShopGradeId()!=null){
					price.setServiceItemPriceId(CodecUtils.getUUID());
					price.setCompId(record.getCompId());
					price.setServiceItemId(record.getServiceItemId());
					
					this.priceMapper.insert(price);
				}
			}
		}
		if(record.getRoleRelaList()!=null&&record.getRoleRelaList().size()>0){
			for(ServiceItemRoleRela roleRela:record.getRoleRelaList()){
				if(roleRela!=null&&roleRela.getRoleId()!=null){
					roleRela.setServiceItemRoleRelaId(CodecUtils.getUUID());
					roleRela.setCompId(record.getCompId());
					roleRela.setServiceItemId(record.getServiceItemId());
					
					this.roleRelaMapper.insert(roleRela);
				}
			}
		}
		
	}

	/**
	 * 逻辑删除
	 */
	@Override
	public void delete(String id) {
		// TODO Auto-generated method stub
		ServiceItem serviceItem = this.getBaseMapper().selectByPrimaryKey(id);
		serviceItem.setState(Constant.STATE_DELETE);
		serviceItem.setStatedate(new Date());
		this.getBaseMapper().updateByPrimaryKey(serviceItem);
	}
	
	public ServiceItem getItemWithDetail(String serviceItemId){
		ServiceItem item = this.getById(serviceItemId);
		if(item!=null){
			item.setPriceList(this.priceMapper.selectWithNoPackageByItemId(serviceItemId));
			item.setRoleRelaList(this.roleRelaMapper.selectByItemId(serviceItemId));
		}
		return item;
	}
	
	public List<ServiceItemRoleRela> getRoleRelaByServiceItemId(String serviceItemId){
		return this.roleRelaMapper.selectByItemId(serviceItemId);
	}
	
	/**
	 * 获取所有正常的服务项目
	 * @param compId
	 * @return
	 */
	public List<ServiceItem> getAllNormalServiceItem(String compId){
		return this.getBaseMapper().queryByPage(new ServiceItem(), null);
	}
	
	public ServiceItem getOne(ServiceItem record){
		return this.getBaseMapper().selectOne(record);
	}
	
	/**
	 * 获取服务项目价格，不包含套餐
	 * @param shopId
	 * @return
	 */
	public List<ServiceItemPrice> getPriceListByShop(String shopId){
		return this.priceMapper.selectByShopId(shopId);
	}
	
	/**
	 * 根据员工和门店获取服务项目
	 * @param employeeId
	 * @param shopId
	 * @return
	 */
	public List<ServiceItem> getItemListByEmployAndShop(String employeeId,String shopId){
		return this.getBaseMapper().selectByEmployAndShop(employeeId, shopId);
	}
	
	/**
	 * 获取套餐的服务项目价格
	 * @param shopId
	 * @return
	 */
	public List<ServiceItemPrice> getPriceListByShopAndPackage(String shopId,String packageId){
		return this.priceMapper.selectByShopIdAndPackage(shopId, packageId);
	}
}
